#ifndef COMMONS_H
#define COMMONS_H
#include <regex>
#include <string>
#include "../third_party/magic_enum/magic_enum.hpp"
#include <variant>

namespace abc
{

    template <typename T, typename P>
    inline T *getAs(P &p)
    {
        if (auto ptr = std::get_if<T *>(&p))
        {
            return *ptr; // 解引用得到存储的指针
        }
        return nullptr;
    };

    template <typename E>
    std::string getEnumTypeName(E type)
    {
        return std::string(magic_enum::enum_name(type));
    }

    inline bool validateAndExtractPath(const std::string &input, std::vector<std::string> &extractedParts)
    {
        // 定义更严格的正则表达式模式
        std::regex pattern(R"(&([a-zA-Z_][a-zA-Z0-9_]*)/src/main/ets(/([a-zA-Z0-9_]+))+&([\d\.a-zA-Z]*))");
        std::smatch matches;
        // 尝试匹配输入字符串
        if (std::regex_match(input, matches, pattern))
        {
            extractedParts.clear();

            // 提取第一个部分(entry)
            std::regex prefixRegex(R"(&([a-zA-Z_][a-zA-Z0-9_]*)/src/main/ets)");
            std::smatch prefixMatch;
            if (std::regex_search(input, prefixMatch, prefixRegex))
            {
                extractedParts.push_back(prefixMatch[1].str());
            }
            std::regex pathRegex(R"(.*?/src/main/ets(/.*?)&([\d\.a-zA-Z]*))");
            std::smatch pathMatch;
            if (std::regex_search(input, pathMatch, pathRegex))
            {
                extractedParts.push_back(pathMatch[1].str());
            }
            return true;
        }

        std::regex patternShort(R"(&([a-zA-Z_][a-zA-Z0-9_]*)/([a-zA-Z0-9_]+)&([\d\.a-zA-Z]*))");
        std::smatch matchesShort;
        if (std::regex_match(input, matchesShort, patternShort))
        {
            extractedParts.clear();
            extractedParts.push_back(matchesShort[1]);
            extractedParts.push_back(matchesShort[2]);
            return true;
        }
        return false;
    }

} // namespace abc_commons
#endif